package com.run2code.design.creational.prototype;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

/**
 * 原型模式
 * 关注公众号【奔跑的码畜】，一起进步不迷路
 *
 * @author 第七人格
 * @date 2023/11/21
 */
public class RpcData implements Cloneable, Serializable {

    private String name;
    private String age;
    private Pet pet;

    public static class Pet implements Serializable {

        private String name;
        private String age;
        private String tyep;

        public Pet() {
        }

        public Pet(String name, String age, String tyep) {
            this.name = name;
            this.age = age;
            this.tyep = tyep;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getAge() {
            return age;
        }

        public void setAge(String age) {
            this.age = age;
        }

        public String getTyep() {
            return tyep;
        }

        public void setTyep(String tyep) {
            this.tyep = tyep;
        }

        @Override
        public String toString() {
            System.out.println("Pet{" +
                    "name='" + name + '\'' +
                    ", age='" + age + '\'' +
                    ", tyep='" + tyep + '\'' +
                    '}');
            return super.toString();
        }
    }

    @Override
    public Object clone() throws CloneNotSupportedException {
        return super.clone();
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAge() {
        return age;
    }

    public void setAge(String age) {
        this.age = age;
    }

    public Pet getPet() {
        return pet;
    }

    public void setPet(Pet pet) {
        this.pet = pet;
    }

    @Override
    public String toString() {
        System.out.println("RpcData{" +
                "name='" + name + '\'' +
                ", age='" + age + '\'' +
                ", pet=" + pet +
                '}');
        return super.toString();
    }

    public Object deepCopy(Object object) throws IOException, ClassNotFoundException {
        ByteArrayOutputStream bo = new ByteArrayOutputStream();
        ObjectOutputStream oo = new ObjectOutputStream(bo);
        oo.writeObject(object);

        ByteArrayInputStream bi = new ByteArrayInputStream(bo.toByteArray());
        ObjectInputStream oi = new ObjectInputStream(bi);

        return oi.readObject();
    }
}
